package com.lemon.smartsafety.ui.activity.safe.inspection;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lemon.android.http.Field;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.event.inspection.InspectionCheckItemDetailRefreshEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingCheckItemVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingIndexVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingItemCountVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingVo;
import com.lemon.smartsafety.entity.safe.inspection.FireCheckLastDetailVo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.InspectionServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 安全巡查 安全巡查【检查项目详情】
 */
public class CheckItemRepairActivity extends SwipeBackActivity {


    @Bind(R.id.tv_tip)
    TextView         tvTip;
    @Bind(R.id.metRemark)
    MaterialEditText metRemark;
    @Bind(R.id.btn_submit)
    Button           btnSubmit;
    @Bind(R.id.btn_report)
    Button           btnReport;


    private LoggerHelper loggerHelper = new LoggerHelper(CheckItemRepairActivity.class);

    /**
     * bundle info
     */
    private CompanyBuildingVo          mCompanyBuildingVo;
    private CompanyBuildingIndexVo     mCompanyBuildingIndexVo;
    private CompanyBuildingItemCountVo mCompanyBuildingItemCountVo;
    private CompanyBuildingCheckItemVo mCompanyBuildingCheckItemVo;

    private String sCheckId = "";

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mCompanyBuildingVo = (CompanyBuildingVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILD_INFO);
            mCompanyBuildingIndexVo = (CompanyBuildingIndexVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_INDEX_VO);
            mCompanyBuildingItemCountVo = (CompanyBuildingItemCountVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_ITEM_COUNT);
            mCompanyBuildingCheckItemVo = (CompanyBuildingCheckItemVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_CHECK_ITEM_VO);
            sCheckId = getIntent().getStringExtra(BundleKey.SAFE_INSPECTION_CHECK_ID);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_check_item_repair_prop;
    }

    @Override
    protected void initViews() {
        super.initViews();

        if (null == mCompanyBuildingCheckItemVo || sCheckId == null || sCheckId.length() == 0) {
            mHUDUtil.showErrorTip(getString(R.string.params_error), new HUDUtil.CallBack() {
                @Override
                public void onCall() {
                    callFinish();
                }
            });
        }

        tvTip.setText(DataInfo.getPositionTitle(mCompanyBuildingVo, mCompanyBuildingIndexVo, mCompanyBuildingItemCountVo, mCompanyBuildingCheckItemVo));
    }


    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.safe_title);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(CheckItemRepairActivity.this);

    }


    @Subscribe
    public void onEventMainThread(HttpCallbackEvent<FireCheckLastDetailVo> event) {
        if (EventTag.TAG_INSPECTION_CHECK_ITEM_REPAIR.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.showInfoTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            InspectionCheckItemDetailRefreshEvent refreshEvent = new InspectionCheckItemDetailRefreshEvent();
                            refreshEvent.setTag(InspectionCheckItemDetailRefreshEvent.class.getName());
                            AppService.getBus().post(refreshEvent);
                            callFinish();
                        }
                    });
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                }
            }
        }
    }


    /**
     * 表单验证
     *
     * @return
     */
    private boolean validateForm() {
        if (metRemark.getText().toString().trim().length() == 0) {

            mHUDUtil.showErrorTip("请填写故障描述");

            return false;
        }
        return true;
    }


    @OnClick({R.id.btn_submit, R.id.btn_report})
    public void onclickAction(View view) {
        if (!validateForm()) {
            return;
        }
        if (view.getId() == R.id.btn_submit) {
            getConfirmDialog("是否提交？", new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        doSubmitAction("1");
                    }
                }
            }).show();
        } else {
            getConfirmDialog("是否提交？", new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        doSubmitAction("2");
                    }
                }
            }).show();
        }

    }

    private void doSubmitAction(String status) {
        LemonRequestParams params = new LemonRequestParams("repair_report", "report_id", "");
        params.put("_txtIdValues", "");
        params.put("report_id", "");

        params.put("txtStatus", status, Field.TYPE_INT);
        params.put("txtCheckId", sCheckId, Field.TYPE_INT);
        params.put("txtReporterId", mSharedPreferencesHelper.getUserId(), Field.TYPE_STRING);
        params.put("txtItemId", mCompanyBuildingCheckItemVo.getItemId(), Field.TYPE_INT);
        params.put("txtReportDate", DateUtils.getCurrentYMDHMSDate(), Field.TYPE_DATETIME);
        params.put("txtReportContent", metRemark.getText().toString(), Field.TYPE_STRING);

        InspectionServices.init().saveCheckItemRepair(params);

    }

    /****************************************************************************************************************
     * 保护现场
     ****************************************************************************************************************/
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(BundleKey.COMPANY_BUILD_INFO, mCompanyBuildingVo);
        outState.putSerializable(BundleKey.COMPANY_BUILDING_ITEM_COUNT, mCompanyBuildingItemCountVo);
        outState.putSerializable(BundleKey.COMPANY_BUILDING_INDEX_VO, mCompanyBuildingIndexVo);
        outState.putSerializable(BundleKey.COMPANY_BUILDING_CHECK_ITEM_VO, mCompanyBuildingCheckItemVo);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            mCompanyBuildingVo = (CompanyBuildingVo) savedInstanceState.getSerializable(BundleKey.COMPANY_BUILD_INFO);
            mCompanyBuildingIndexVo = (CompanyBuildingIndexVo) savedInstanceState.getSerializable(BundleKey.COMPANY_BUILDING_INDEX_VO);
            mCompanyBuildingItemCountVo = (CompanyBuildingItemCountVo) savedInstanceState.getSerializable(BundleKey.COMPANY_BUILDING_ITEM_COUNT);
            mCompanyBuildingCheckItemVo = (CompanyBuildingCheckItemVo) savedInstanceState.getSerializable(BundleKey.COMPANY_BUILDING_CHECK_ITEM_VO);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_INSPECTION_CHECK_ITEM_RECORD);
        mCompanyBuildingVo = null;
        mCompanyBuildingIndexVo = null;
        mCompanyBuildingItemCountVo = null;
        mCompanyBuildingCheckItemVo = null;
    }
}
